import { createRouter, createWebHashHistory } from 'vue-router'
import routerMap from './routerMap'
import storage from '@/utils/storage'
const Router = createRouter({
  history: createWebHashHistory(),
  routes: routerMap,
})
Router.beforeEach(async (to, from, next) => {
  let token = storage.get('token')

  if (to.path === '/login') {
    if (token) {
      next('/lay')
    } else {
      next()
    }
  } else {
    if (token) {
      if (to.matched.length === 0) {
        next({...to, replace: true})
      } else {
        next()
      }
    } else {
      next('/login')
    }
  }
})
export default Router
